home *** CD-ROM | disk | FTP | other *** search
/ Ahoy: Best Utilites / Ahoy_Best_Utilites_1986_Double_L.d64 / base conversion (.txt) < prev    next >
Encoding:
Commodore BASIC  |  1986-01-01  |  3.1 KB  |  113 lines

  1. 1 rem**********************************
  2. 2 rem*this program will exchange binary
  3. 3 rem*hexadecimal and decimal notation
  4. 4 rem* lines 10 - 80 display the menu        * and get the function number
  5. 5 rem**********************************
  6. 6 poke53281,7:n$="":e$=""
  7. 7 print"[147]"
  8. 8 print e$
  9. 10 print"chose a function 1-7[146]"
  10. 20 print"1 binary to decimal"
  11. 30 print"2 binary to hexadecimal"
  12. 40 print"3 hexadecimal to decimal"
  13. 50 print"4 hexadecimal to binary"
  14. 60 print"5 decimal to binary"
  15. 70 print"6 decimal to hexadecimal"
  16. 75 print"7 exit":input a:if(a>7)or(a<1)thene$="nonexistant function":goto 7
  17. 77 if a=7 then end
  18. 80 print"enter your number":e$=""
  19. 81 rem*********************************       * the next few lines accept the
  20. 82 rem* value into a string variable          * and send it to the subroutines
  21. 83 rem* for conversion                        *********************************
  22. 90 input n$:on a gosub 1000,200,1200,400,1300,1100
  23. 95 print"[147]the answer is "n$:print:print"press any key to go to menu"
  24. 96 getxz$:ifxz$=""then goto96
  25. 97 goto 7
  26. 200 gosub 1000
  27. 210 gosub 1100
  28. 220 return
  29. 400 gosub 1200
  30. 410 gosub 1300
  31. 420 return
  32. 1000 rem******************************
  33. 1005 rem*this subroutine converts
  34. 1010 rem* binary to decimal
  35. 1015 rem*****************************
  36. 1020 l=len(n$):dn=0:d$=""
  37. 1030 if l>16 then e$="data too large":goto 7
  38. 1035 forx=1 to l
  39. 1040 ifnot(mid$(n$,x,1)="0"ormid$(n$,x,1)="1")then e$="bad bin data":goto 7
  40. 1045 next
  41. 1050 for x=lto1 step-1
  42. 1055 ch$=mid$(n$,x,1)
  43. 1060 ch=0
  44. 1065 if ch$="1" then ch=1
  45. 1070 dn =dn+(2^(l-x)*ch)
  46. 1075 next x
  47. 1080 n$=str$(dn):forx=1 to len(n$)
  48. 1085 if not mid$(n$,x,1)=" "then d$=d$+mid$(n$,x,1)
  49. 1090 next:n$=d$:return
  50. 1100 rem********************************
  51. 1101 rem*this subroutine will convert
  52. 1102 rem*decimal to hexadecimal
  53. 1103 rem******************************
  54. 1104 l=len(n$)
  55. 1105 for x=1 to l:c$=mid$(n$,x,1)
  56. 1110 ifasc(c$)<48orasc(c$)>57thene$="illegal decimal data":goto 7
  57. 1115 next x:if val(n$)>65535then e$="number too large":goto 7
  58. 1120 d=val(n$):nd=1:n$=""
  59. 1125 if 16^nd>=d+1then goto 1135
  60. 1130 nd=nd+1:goto 1125
  61. 1135 for x=nd to 1 step-1
  62. 1145 c=int(d/16^(x-1))
  63. 1150 d=d-c*16^(x-1)
  64. 1155 if c=15 then n$=n$+"f"
  65. 1160 if c=14 then n$=n$+"e"
  66. 1165 if c=13 then n$=n$+"d"
  67. 1170 if c=12 then n$=n$+"c"
  68. 1175 if c=11 then n$=n$+"b"
  69. 1180 if c=10 then n$=n$+"a"
  70. 1185 if c<10 then n$=n$+str$(c)
  71. 1190 next x :d$="":for x=1 to len(n$)
  72. 1195 ifnotmid$(n$,x,1)=" "thend$=d$+mid$(n$,x,1)
  73. 1196 next:n$=d$:return
  74. 1200 rem*******************************
  75. 1201 rem* this subroutine will convert          * hexadecimal to decimal
  76. 1202 rem*******************************
  77. 1204 forx=1 to len(n$):d$=mid$(n$,x,1)
  78. 1205 ifasc(d$)<48 or asc(d$)>70thene$="illegal hex data":goto 7
  79. 1206 ifasc(d$)<65andasc(d$)>57 then e$="illegal hexadecimal data" goto 7
  80. 1207 if len(n$)>4 then e$="number too large":goto 7
  81. 1208 next
  82. 1209 n=0:d$=""
  83. 1210 for x=0 to len(n$)-1
  84. 1215 ch$=mid$(n$,len(n$)-x,1)
  85. 1220 if ch$="f" then f=15
  86. 1225 if ch$="e" then f=14
  87. 1230 if ch$="d" then f=13
  88. 1235 if ch$="c" then f=12
  89. 1240 if ch$="b" then f=11
  90. 1245 if ch$="a" then f=10
  91. 1250 if asc(ch$)<58 then f=val(ch$)
  92. 1255 n=n+f*16^x
  93. 1260 next x
  94. 1265 n$=str$(n)
  95. 1270 for x=1 to len(n$)
  96. 1280 ifnot mid$(n$,x,1)=" "then d$=d$+mid$(n$,x,1)
  97. 1285 next:n$=d$:return
  98. 1299 rem********************************        * this subroutine will convert
  99. 1300 rem* decimal to binary                     *******************************
  100. 1301 forx=1 to len(n$)
  101. 1302 ifasc(mid$(n$,x,1))>57orasc(mid$(n$,x,1))<48then e$="bad dec data":goto7
  102. 1303 next:ifval(n$)>65535then e$="number too large":goto 7
  103. 1309 bc=0:c=val(n$):n$=""
  104. 1310 if 2^bc<c then bc=bc+1:goto 1310
  105. 1315 for x=0 to bc
  106. 1320 c$="1"
  107. 1325 if c<2^bc then c$="0"
  108. 1330 if c>=2^bc then c=c-2^bc
  109. 1335 bc=bc-1
  110. 1340 n$=n$+c$
  111. 1345 next x
  112. 1350 return
  113.